package com.cloud.server.service.user.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cloud.server.from.login.FromLoginUser;
import com.cloud.server.domain.user.User;
import com.cloud.server.domain.user.UserIntegral;
import com.cloud.server.from.login.LoginType;
import com.cloud.server.mapper.user.UserMapper;
import com.cloud.server.service.user.LoginService;
import com.cloud.server.service.user.UserIntegralService;
import com.cloud.server.service.user.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liuyuan
 * @since 2021-11-17
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    public static Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    UserService userService;
    @Autowired
    UserIntegralService userIntegralService;

    @Override
    @Transactional
    public boolean saveStudent(User user) throws Exception {
        boolean userSave = userService.save(user);
        if(userSave){
            User userOne = getUser(user);
            UserIntegral userIntegral = new UserIntegral();
            userIntegral.setUserId(userOne.getId());
            userIntegral.setLevel(1);
            userIntegral.setIntegralTotal(0L);
            boolean userIntegralSave = userIntegralService.save(userIntegral);
            if (userIntegralSave){
                return true;
            }else {
                throw new Exception("注册失败");
            }
        }else {
            throw new Exception("注册失败");
        }

    }

    private User getUser(User user) {
        User queryUser = new User();
        queryUser.setIdCardNo(user.getIdCardNo());
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.setEntity(queryUser);
        return userService.getOne(userQueryWrapper);

    }


}
